package com.zwk.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import com.zwk.views.*;
import com.zwk.model.*;

public class WKLoginView extends JFrame{
 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 300;
	public static JFrame frame;
	JFrame parentFrame;
	public WKLoginView(String title) {
		frame = new JFrame();
		frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		frame.setTitle(title);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidthpx = screenSize.width;
		int screenHeightpx = screenSize.height;
		frame.setLocation(screenWidthpx / 3, screenHeightpx / 3);
		frame.setLocationByPlatform(false);
		
		Image image = new ImageIcon(this.getClass().getResource("/WKImage/top1.jpg")).getImage();
		frame.setIconImage(image);
		
		WKLoginPanel loginPanel = new WKLoginPanel();
		frame.setContentPane(loginPanel);
		frame.setVisible(true);
		
	}
	private class WKLoginPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JLabel picLabel;
	    JLabel idLabel, pwdLabel;
	    JTextField stuIdTextField;
	    JPasswordField pwdField;
	    JButton okButton, clearButton;
	    JPanel buttonPanel, infoPanel;
	    
	    public WKLoginPanel() {
	    	picLabel = new JLabel();
	    	idLabel = new JLabel("学生ID:");
	    	pwdLabel = new JLabel("密   码:");
	    	stuIdTextField = new JTextField(20);
	    	pwdField = new JPasswordField(20);
	    	okButton = new JButton("登陆");
	    	okButton.addActionListener(new ActionListener(){
	    		@Override
	    		public void actionPerformed(ActionEvent e) {
	    			EventQueue.invokeLater(new Runnable() {

						@SuppressWarnings("deprecation")
						@Override
						public void run() {
							// TODO Auto-generated method stub
						    if (WKStdLoginOperator.isTrue(stuIdTextField.getText(), pwdField.getText())) {
						    	System.out.println("ture");
						    	WKStdFuncPortalFrame stdFuncView = new WKStdFuncPortalFrame(stuIdTextField.getText());
						    	stdFuncView.parentFrame = WKLoginView.this;
						    	frame.dispose();
						    } else {
						    	System.out.println("false");
						    	JOptionPane.showMessageDialog(null, "请重新输入密码", "密码错误", JOptionPane.ERROR_MESSAGE);
						    	pwdField.setText("");
						    }
						}
	    			});
	    		}
	    	}
	    	);
	    	clearButton = new JButton("清空信息");
	    	clearButton.addActionListener(new ActionListener() {
	    		@Override 
	    		public void actionPerformed(ActionEvent e) {
	    			stuIdTextField.setText("");
	    			pwdField.setText("");
	    		}
	    	}
	    	);
	    	
	    	buttonPanel = new JPanel();
	    	infoPanel = new JPanel();
	    	setLayout(new BorderLayout());
	    	
	    	infoPanel.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
	    	picLabel.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT/2);
	    	
	    	ImageIcon image = new ImageIcon(this.getClass().getResource("/WKImage/top1.jpg"));
	    	image.setImage(image.getImage().getScaledInstance(picLabel.getWidth(), picLabel.getHeight() + 30, Image.SCALE_DEFAULT));;
	    	picLabel.setIcon(image);
	    	infoPanel.add(picLabel);
	    	infoPanel.add(idLabel);
	    	infoPanel.add(stuIdTextField);
	    	infoPanel.add(pwdLabel);
	    	infoPanel.add(pwdField);
	    	add(infoPanel, BorderLayout.CENTER);
	    	buttonPanel.add(okButton);
	    	buttonPanel.add(clearButton);
	    	add(buttonPanel, BorderLayout.SOUTH);
	    }
	
	}
	
}
